home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 5 code / Palette Manager Animation / GiMeDaPalette.c next >
Encoding:
C/C++ Source or Header  |  1991-09-09  |  14.2 KB  |  506 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                       Palette Demo                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*              Developer Technical Support             */
  6. /*                 Apple Computer, Inc.                 */
  7. /*                       08/15/90                       */
  8. /*______________________________________________________*/
  9.  
  10. #include    <CType.h>
  11. #include    <Quickdraw.h>
  12. #include    <Windows.h>
  13. #include    <desk.h>
  14. #include    <dialogs.h>
  15. #include    <Events.h>
  16. #include     <GestaltEqu.h>
  17. #include    <Memory.h>
  18. #include    <Menus.h>
  19. #include    <OSEvents.h>
  20. #include    <Palette.h>
  21. #include    <SegLoad.h>
  22. #include    <Resources.h>
  23. #include    <ToolUtils.h>
  24. #include    <StdIO.h>
  25. #include    <QDOffscreen.h>
  26.  
  27. extern _DataInit();
  28.  
  29. #define pmInhibitG2        0x0100
  30. #define pmInhibitC2        0x0200
  31. #define pmInhibitG4        0x0400
  32. #define pmInhibitC4        0x0800
  33. #define pmInhibitG8        0x1000
  34. #define pmInhibitC8        0x2000
  35.  
  36. #define    TRUE            0xFF
  37. #define    FALSE            0
  38. #define Gestalttest        0xA1AD
  39. #define NoTrap            0xA89F
  40.  
  41. #define    appleID            128            
  42. #define    appleMenu        0
  43. #define    aboutMeCommand    1
  44.  
  45. #define    fileID            129
  46. #define    quitCommand     1
  47.  
  48. #define    PaletteID        130
  49. #define    CourCommand     1
  50. #define    TolCommand         2
  51. #define    ExpCommand         3
  52. #define    AniCommand         4
  53. #define    TolExpCommand     6
  54. #define    TolAniCommand     7
  55. #define    ExpAniCommand     8
  56. #define    TEACommand         10
  57.  
  58. #define    PictID            128
  59. #define    clutID            150            // If you would like gray scale then change 150 to 36
  60.  
  61. #define    aboutMeDLOG        128
  62. #define    okButton        1
  63. #define    authorItem        2
  64. #define    languageItem    3
  65. #define    numcolor        256
  66.  
  67. Rect                TotalRect, minRect, WinMinusScroll, InitWindowSize;
  68. WindowPtr            myWindow;
  69. CTabHandle            mycolors;
  70. PaletteHandle        srcPalette;
  71. Boolean                DoneFlag;
  72. MenuHandle            mymenu1, mymenu2, mymenu0;
  73. PicHandle            ThePict;
  74. GWorldPtr            offscreenGWorld;
  75.  
  76. /*______________________________________________________*/
  77. /*                Check Correct Menu                    */
  78. /*______________________________________________________*/
  79. void CheckMenu(int whichMenu)
  80. /* This procedure checks and unchecks the appropriate menus */
  81. {
  82.             CheckItem(mymenu2,CourCommand,FALSE);
  83.             CheckItem(mymenu2,TolCommand,FALSE);
  84.             CheckItem(mymenu2,ExpCommand,FALSE);
  85.             CheckItem(mymenu2,AniCommand,FALSE);
  86.             CheckItem(mymenu2,TolExpCommand,FALSE);
  87.             CheckItem(mymenu2,TolAniCommand,FALSE);
  88.             CheckItem(mymenu2,ExpAniCommand,FALSE);
  89.             CheckItem(mymenu2,TEACommand,FALSE);
  90.     switch (whichMenu) {
  91.         case CourCommand:
  92.             CheckItem(mymenu2,CourCommand,TRUE);
  93.             break;
  94.         case TolCommand:
  95.             CheckItem(mymenu2,TolCommand,TRUE);
  96.             break;
  97.         case ExpCommand:
  98.             CheckItem(mymenu2,ExpCommand,TRUE);
  99.             break;
  100.         case AniCommand:
  101.             CheckItem(mymenu2,AniCommand,TRUE);
  102.             break;
  103.         case TolExpCommand:
  104.             CheckItem(mymenu2,TolExpCommand,TRUE);
  105.             break;
  106.         case TolAniCommand:
  107.             CheckItem(mymenu2,TolAniCommand,TRUE);
  108.             break;
  109.         case ExpAniCommand:
  110.             CheckItem(mymenu2,ExpAniCommand,TRUE);
  111.             break;
  112.         case TEACommand:
  113.             CheckItem(mymenu2,TEACommand,TRUE);
  114.             break;
  115.         defalut:
  116.             break;
  117.         }
  118. }
  119.  
  120. /*______________________________________________________*/
  121. /*             Set Up Usage of Palette                  */
  122. /*______________________________________________________*/
  123. void Draw()
  124. {
  125.     RGBColor        black = {0,0,0}, White = {65535,65535,65535};
  126.         
  127.     RGBForeColor (&black);
  128.     RGBBackColor (&White);
  129.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  130.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  131. }
  132.  
  133. /*______________________________________________________*/
  134. /*             Set Up Usage of Palette                  */
  135. /*______________________________________________________*/
  136. void SetInhibited(int Usage)
  137. /* When the usage of the palette changes I need to rebuild the palette,
  138. so I call this routine.  I need to call SetEntryUsage on all of the entries
  139. of the palette, but I want to make sure that if the monitor is set to a bit depth
  140. low than 8 bits that the palette is limited in its colors.  So the first two colors
  141. (not including white) are set to work at all depths except 1 bit mode.  The next 
  142. 12 entries are set to work on all depths except 1 & 2 bit modes.  The rest of 
  143. the colors are set to only work in 8 bit mode.  I have also set the tolerance to
  144. hex 1500.  With this tolerance I will be able to get most of the colors I need 
  145. and those that I don't get exact will be very close. */
  146. {
  147.     int        i;
  148.     
  149.     CTab2Palette (mycolors, srcPalette, Usage, 0x1500);
  150.     for (i = 1; i <= 2; ++i) {
  151.         SetEntryUsage (srcPalette, i, Usage, 0x1500);
  152.         }
  153.     for (i = 3; i <= 14; ++i) {
  154.         SetEntryUsage (srcPalette, i, Usage+pmInhibitG2+pmInhibitC2, 0x1500);
  155.         }
  156.     for (i = 15; i < numcolor; ++i) {
  157.         SetEntryUsage (srcPalette, i, Usage+pmInhibitG2+pmInhibitC2+
  158.                         pmInhibitG4+pmInhibitC4, 0x1500);
  159.         }
  160. }
  161.  
  162. /*______________________________________________________*/
  163. /*                 About Prog Dialog                   */
  164. /*______________________________________________________*/
  165. void showAboutMeDialog()
  166. {
  167.     GrafPtr     savePort;
  168.     DialogPtr    theDialog;
  169.     short        itemHit;
  170.  
  171.     GetPort(&savePort);
  172.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  173.     SetPort(theDialog);
  174.  
  175.     do {
  176.         ModalDialog(nil, &itemHit);
  177.     } while (itemHit != okButton);
  178.  
  179.     CloseDialog(theDialog);
  180.  
  181.     SetPort(savePort);
  182.     return;
  183. }
  184.  
  185. /*______________________________________________________*/
  186. /*                 Do Menu Function                     */
  187. /*______________________________________________________*/
  188. void doCommand(mResult)
  189.     long    mResult;
  190. {
  191.     int                     theMenu, theItem;
  192.     char                    daName[256];
  193.     GrafPtr                 savePort;
  194.  
  195.     theItem = LoWord(mResult);
  196.     theMenu = HiWord(mResult);
  197.     
  198.     switch (theMenu) {
  199. /*______________________________________________________*/
  200. /*                    Do Apple Menu                     */
  201. /*______________________________________________________*/
  202.         case appleID:
  203.             if (theItem == aboutMeCommand)
  204.                 showAboutMeDialog();
  205.             else {
  206.                 GetItem(mymenu0, theItem, daName);
  207.                 GetPort(&savePort);
  208.                 (void) OpenDeskAcc(daName);
  209.                 SetPort(savePort);
  210.             }
  211.             break;
  212. /*______________________________________________________*/
  213. /*                     Do File Menu                     */
  214. /*______________________________________________________*/
  215.         case fileID:
  216.             switch (theItem) {
  217.                 case quitCommand:
  218.                     DoneFlag = TRUE;
  219.                     break;
  220.                 default:
  221.                     break;
  222.                 }
  223.             break;
  224.  
  225. /*______________________________________________________*/
  226. /*                     Do File Menu                     */
  227. /*______________________________________________________*/
  228.         case PaletteID:
  229.             switch (theItem) {
  230.                 case CourCommand:
  231.                     CheckMenu(CourCommand);
  232.                     SetInhibited(pmCourteous);
  233.                     break;
  234.                 case TolCommand:
  235.                     CheckMenu(TolCommand);
  236.                     SetInhibited(pmTolerant);
  237.                     break;
  238.                 case ExpCommand:
  239.                     CheckMenu(ExpCommand);
  240.                     SetInhibited(pmExplicit);
  241.                     break;
  242.                 case AniCommand:
  243.                     CheckMenu(AniCommand);
  244.                     SetInhibited(pmAnimated);
  245.                     break;
  246.                 case TolExpCommand:
  247.                     CheckMenu(TolExpCommand);
  248.                     SetInhibited(pmTolerant+pmExplicit);
  249.                     break;
  250.                 case TolAniCommand:
  251.                     CheckMenu(TolAniCommand);
  252.                     SetInhibited(pmTolerant+pmAnimated);
  253.                     break;
  254.                 case ExpAniCommand:
  255.                     CheckMenu(ExpAniCommand);
  256.                     SetInhibited(pmAnimated+pmExplicit);
  257.                     break;
  258.                 case TEACommand:
  259.                     CheckMenu(TEACommand);
  260.                     SetInhibited(pmTolerant+pmExplicit+pmAnimated);
  261.                     break;
  262.                 default:
  263.                     break;
  264.                 }
  265.             SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  266.             ActivatePalette ((WindowPtr) myWindow);
  267.             break;
  268.         }
  269.     HiliteMenu(0);
  270.     return;
  271. }
  272.  
  273. /*______________________________________________________*/
  274. /*               Initialization traps                   */
  275. /*______________________________________________________*/
  276. void init()
  277. {
  278.     RgnHandle            tempRgn;
  279.     Rect                BaseRect;
  280.     OSErr                err;
  281.     long                QDfeature, OSfeature;
  282.     GDHandle            SaveGD;
  283.     CGrafPtr            SavePort;
  284.     RGBColor            black = {0,0,0}, White = {65535,65535,65535};
  285.  
  286.     UnloadSeg(_DataInit);
  287.     InitGraf(&qd.thePort);
  288.     FlushEvents(everyEvent, 0);
  289.     InitWindows();
  290.     InitDialogs(nil);
  291.     InitCursor();
  292.     
  293.     DoneFlag = FALSE;
  294.     ThePict = GetPicture(PictID);
  295.     if (ThePict == nil)
  296.         DoneFlag = TRUE;
  297.  
  298. /*______________________________________________________*/
  299. /*            Use Gestalt to find 32BQD                 */
  300. /*______________________________________________________*/
  301. if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  302.     err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  303.     if (err && (QDfeature & 0x0f00) < 0x0200)
  304.         DoneFlag = TRUE;
  305.     err = Gestalt(gestaltSystemVersion, &OSfeature);
  306.     if (err)
  307.         DoneFlag = TRUE;
  308.     if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0605)
  309.         DoneFlag = TRUE;
  310.     }
  311. else
  312.     DoneFlag = TRUE;
  313. /*______________________________________________________*/
  314. /*                     Set Rects                        */
  315. /*______________________________________________________*/
  316. if (DoneFlag == FALSE) {
  317.     SetRect(&BaseRect, 40, 60, 472, 282);
  318.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  319.                 BaseRect.bottom - 80);
  320.     SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top, 
  321.                             WinMinusScroll.right, WinMinusScroll.bottom);
  322.     tempRgn = GetGrayRgn();
  323.     HLock ((Handle) tempRgn);
  324.     TotalRect = (**tempRgn).rgnBBox;
  325.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  326.                 (**tempRgn).rgnBBox.bottom - 40);
  327.     HUnlock ((Handle) tempRgn);
  328.  
  329. /*______________________________________________________*/
  330. /*        Open Window & set Palette & Picture           */
  331. /*______________________________________________________*/
  332.     GetGWorld (&SavePort, &SaveGD);
  333.     mycolors = GetCTable (clutID);
  334.     (*mycolors)->ctFlags |= 0x4000;
  335.  
  336.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  337.                             (WindowPtr) -1, TRUE, 150);
  338.     SetGWorld((CGrafPtr)myWindow, SaveGD);
  339.     DrawGrowIcon (myWindow);
  340.  
  341.     srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
  342.     SetInhibited(pmCourteous);
  343.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  344.     
  345.     //DrawPicture (ThePict, &InitWindowSize);
  346.  
  347.     GetGWorld (&SavePort, &SaveGD);
  348.     err = NewGWorld (&offscreenGWorld, 8, &InitWindowSize, mycolors, nil, nil);
  349.     if (err)
  350.         Debugger();
  351.     SetGWorld (offscreenGWorld, nil);
  352.     EraseRect (&InitWindowSize);
  353.     DrawPicture (ThePict, &InitWindowSize);
  354.     SetGWorld (SavePort, SaveGD);
  355.         
  356.  
  357. /*______________________________________________________*/
  358. /*                    Set menus                         */
  359. /*______________________________________________________*/
  360.     mymenu0 = GetMenu(appleID);
  361.     AddResMenu(mymenu0, 'DRVR');
  362.     InsertMenu(mymenu0,0);
  363.     mymenu1 = GetMenu(129);
  364.     InsertMenu(mymenu1,0);
  365.     mymenu2 = GetMenu(130);
  366.     InsertMenu(mymenu2,0);
  367.     DrawMenuBar();
  368.     }
  369.     
  370.     return;
  371. }
  372.  
  373. main()
  374. {
  375.     char            key;
  376.     Boolean            track;
  377.     long            growResult;
  378.     EventRecord     myEvent;
  379.     WindowPtr        whichWindow;
  380.     int                yieldTime;
  381.     RGBColor        changecolor;
  382.     CTabHandle        StoreCTab;
  383.     short            Usage, Tolerance;
  384. /*______________________________________________________*/
  385. /*                   Main Event loop                    */
  386. /*______________________________________________________*/
  387.     init();
  388.     yieldTime = 0;
  389.     StoreCTab = GetCTable (clutID);
  390.     for ( ;; ) {
  391.         if (DoneFlag) {
  392.             ExitToShell();
  393.             }
  394.             
  395.         /* If the usage is set to animate than animate the colors one step for eash event loop */
  396.         GetEntryUsage(srcPalette, 1, &Usage, &Tolerance);
  397.         Usage &= 0x0004;
  398.         if (Usage == 0x0004) {
  399.             GetEntryColor (srcPalette, 1, &changecolor);
  400.             AnimatePalette (myWindow, StoreCTab, 2, 1, numcolor - 2);
  401.             AnimateEntry (myWindow, numcolor - 1, &changecolor);
  402.             Palette2CTab (srcPalette, StoreCTab);
  403.             }
  404.             
  405.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  406.             switch (myEvent.what) {
  407.                 case mouseDown:
  408.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  409.                         case inSysWindow:
  410.                             SystemClick(&myEvent, whichWindow);
  411.                             break;
  412.                         case inMenuBar:
  413.                             doCommand(MenuSelect(myEvent.where));
  414.                             break;
  415.                         case inContent:
  416.                             break;
  417.                         case inDrag:
  418.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  419.                             Draw();
  420.                             DrawGrowIcon (whichWindow);
  421.                             break;
  422.                         case inGrow:
  423.                             growResult = GrowWindow (whichWindow, myEvent.where,
  424.                                                     &minRect);
  425.                             SizeWindow(whichWindow, LoWord(growResult), 
  426.                                     HiWord(growResult), TRUE);
  427.                             EraseRect(&whichWindow->portRect);
  428.                             SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  429.                                     whichWindow->portRect.top, 
  430.                                     whichWindow->portRect.right-20, 
  431.                                     whichWindow->portRect.bottom - 20);
  432.                             Draw();
  433.                             DrawGrowIcon (whichWindow);
  434.                             break;
  435.                         case inGoAway:
  436.                             track = TrackGoAway (whichWindow, myEvent.where);
  437.                             if (track) {
  438.                                 CloseWindow (whichWindow);
  439.                                 DoneFlag = TRUE;
  440.                                 }
  441.                             break;
  442.                         case inZoomIn:
  443.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  444.                             if (track) {
  445.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  446.                                 EraseRect(&whichWindow->portRect);
  447.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  448.                                         whichWindow->portRect.top, 
  449.                                         whichWindow->portRect.right-20, 
  450.                                         whichWindow->portRect.bottom - 20);
  451.                                 Draw();
  452.                                 DrawGrowIcon (whichWindow);
  453.                                 }
  454.                             break;
  455.                         case inZoomOut:
  456.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  457.                             if (track) {
  458.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  459.                                 EraseRect(&whichWindow->portRect);
  460.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  461.                                         whichWindow->portRect.top, 
  462.                                         whichWindow->portRect.right-20, 
  463.                                         whichWindow->portRect.bottom - 20);
  464.                                 Draw();
  465.                                 DrawGrowIcon (whichWindow);
  466.                                 }
  467.                             break;
  468.                         default:
  469.                             break;
  470.                         }
  471.                     break;
  472.                 case keyDown:
  473.                 case autoKey:
  474.                     key = myEvent.message & charCodeMask;
  475.                     if ( myEvent.modifiers & cmdKey )
  476.                         if ( myEvent.what == keyDown )
  477.                             doCommand(MenuKey(key));
  478.                     break;
  479.                 case updateEvt:
  480.                     if ((WindowPtr) myEvent.message == myWindow) {
  481.                         BeginUpdate((WindowPtr) myWindow);
  482.                         EndUpdate((WindowPtr) myWindow);
  483.                         Draw();
  484.                         }
  485.                     break;
  486.                 case diskEvt:
  487.                     break;
  488.                 case activateEvt:
  489.                     break;
  490.                 case app4Evt:
  491.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  492.                         yieldTime = 30;
  493.                         HideWindow((WindowPtr) myWindow);
  494.                         }
  495.                     else { /* Resume */
  496.                         yieldTime = 0;
  497.                         ShowWindow((WindowPtr) myWindow);
  498.                         SetPort((WindowPtr) myWindow);
  499.                         }
  500.                     break;
  501.                 default:
  502.                     break;
  503.                 }
  504.             }
  505.         }
  506. }